package com.xk857.util;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一json处理
 * @Date 2020.06.07
 * @author CV大魔王
 */
@Data
public class Result implements Serializable {

    /** 是否成功 */
    private boolean flag;

    /** 返回码 */
    private Integer code;

     /** 返回消息 */
    private String message;

     /** 返回数据 */
    private Object serverInfo;

    public Result(boolean flag, Integer code, String message, Object serverInfo) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.serverInfo = serverInfo;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public Result() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.message = "操作成功!";
    }

    /**
     * 请求成功，返回数据
     * @param data 返回的数据
     */
    public static Result buildSuccess(Object data){
        return new Result(true,StatusCode.OK,"请求成功",data);
    }

    /**
     * 请求成功，不返回数据
     */
    public static Result buildSuccess(){
        return new Result(true,StatusCode.OK,"请求成功");
    }

    /**
     * 请求失败，返回数据
     */
    public static Result buildError(){
        return new Result(false,StatusCode.ERROR,"请求失败");
    }


    /**
     * 请求失败，返回数据
     */
    public static Result buildError(Object data){
        return new Result(false,StatusCode.ERROR,"请求失败",data);
    }
}
